package com.shroo8.footballbloglib.dao.footballblogread;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.stereotype.Repository;

import com.shroo8.footballblog.dto.api.ArticleDto;
import com.shroo8.footballbloglib.dao.constants.DataAccessConstants;
import com.shroo8.footballbloglib.dao.util.DaoUtil;

@Repository(value="getArticlesDao")
public class GetArticlesDao {

	@SuppressWarnings("rawtypes")
	private class OneRowMapper implements RowMapper{
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        	ArticleDto articleDto = new ArticleDto();
            articleDto.setArticleId(rs.getString(DataAccessConstants.ARTICLES_ARTICLE_ID));
            articleDto.setAuthorId(rs.getString(DataAccessConstants.ARTICLES_AUTHOR_ID));
            articleDto.setArticleTitle(rs.getString(DataAccessConstants.ARTICLES_ARTICLE_TITLE));
            articleDto.setArticleText(rs.getString(DataAccessConstants.ARTICLES_ARTICLE_TEXT));
            articleDto.setAverageRating(rs.getDouble(DataAccessConstants.ARTICLES_AVERAGE_RATING));
            articleDto.setNumberOfRatings(rs.getInt(DataAccessConstants.ARTICLES_NUMBER_OF_RATINGS));
            articleDto.setCreatedOn(rs.getTimestamp(DataAccessConstants.ARTICLES_CREATED_ON).toString());
            return articleDto;
        }
    }
	
	protected final String sql = DataAccessConstants.SP_GET_ARTICLES_BY_USER_TYPE_AND_EXTERNAL_ID;

	@Resource(name="dataSourceFootballblogRead")
	protected DataSource dataSource;

	@Resource(name="daoUtil")
	protected DaoUtil daoUtil;

    /* stored procedure parameter list
	(
	 	in_user_type ENUM('twitter','facebook'),
	 	in_user_external_id INT
	)
	*/
	public ArrayList<ArticleDto> execute(String userType, int userExternalId){
		List<SqlParameter> sqlParameters = new ArrayList<SqlParameter>();
	    Map<String, Object> inParams = new HashMap<String, Object>();

        sqlParameters.add(new SqlReturnResultSet("resultSet",
                new OneRowMapper()));
	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_TYPE, Types.VARCHAR));
        inParams.put(DataAccessConstants.USERS_USER_TYPE, userType);

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_EXTERNAL_ID, Types.INTEGER));
        inParams.put(DataAccessConstants.USERS_USER_EXTERNAL_ID, userExternalId);

        Map<String, Object> out = daoUtil.spExecute(dataSource, sql,
                sqlParameters, inParams);

        @SuppressWarnings("unchecked")
		ArrayList<ArticleDto> list = (ArrayList<ArticleDto>) out.get("resultSet");
        return list;
	}
}
